package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.odorservice.entity.Craft;
import com.ruoyi.odorservice.entity.Material;
import com.ruoyi.odorservice.mapper.CraftMapper;
import com.ruoyi.odorservice.mapper.MaterialMapper;
import com.ruoyi.odorservice.model.vo.CraftOdorMaterialVo;
import com.ruoyi.odorservice.model.vo.OdorMaterialVo;
import com.ruoyi.odorservice.model.vo.SearchMaterialVo;
import com.ruoyi.odorservice.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {

    @Autowired
    private CraftMapper craftMapper;

    @Override
    public Result<Material> searchMaterial(Integer id) {
        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Material::getMaterialId, id);
        Material material = this.baseMapper.selectOne(wrapper);
        return Result.ok(material);
    }

    @Override
    public Result<List<Material>> searchOdor(Integer id) {
        List<Material> materials = this.baseMapper.selectMaterialListByOdor(id);
        return Result.ok(materials);
    }

    @Override
    public Result<List<CraftOdorMaterialVo>> searchIndustry(Integer id) {

        List<CraftOdorMaterialVo> craftOdorMaterial = craftMapper.selectCraftByIndustryId(id);

        List<Integer> odorIds = new ArrayList<>();

        for (CraftOdorMaterialVo cm : craftOdorMaterial) {
            List<OdorMaterialVo> omlist = cm.getOdorMaterial();
            for (OdorMaterialVo om : omlist) {
                odorIds.add(om.getOdorId());
            }
        }
        if(odorIds.isEmpty()) odorIds.add(-1);
        List<OdorMaterialVo> odorMaterial = this.baseMapper.selectOdorMaterialByOdorIds(odorIds);
        for (CraftOdorMaterialVo cm : craftOdorMaterial) {
            List<OdorMaterialVo> omlist = cm.getOdorMaterial();
            for (OdorMaterialVo om : omlist) {
                for(OdorMaterialVo om2 : odorMaterial){
                    if(om.getOdorId().equals(om2.getOdorId())){
                        om.setMaterialList(om2.getMaterialList());
                        break;
                    }
                }
            }
        }
        return Result.ok(craftOdorMaterial);
    }
    @Override
    public List<SearchMaterialVo> searchMaterial(String key) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<Material>()
                .select(Material::getMaterialId, Material::getMaterialName, Material::getMainApplication)
                .like(StringUtils.isNotEmpty(key), Material::getMaterialName, key)
                .or()
                .like(StringUtils.isNotEmpty(key), Material::getMaterialNameEn, key)
                .or()
                .like(StringUtils.isNotEmpty(key), Material::getCasNo, key)
                .last("limit 5");
        List<Material> materialList = list(queryWrapper);

        List<SearchMaterialVo> searchMaterialVoList = new ArrayList<>();
        for (Material material : materialList) {
            SearchMaterialVo searchMaterialVo = new SearchMaterialVo();
            BeanUtils.copyProperties(material, searchMaterialVo);
            searchMaterialVoList.add(searchMaterialVo);
        }
        return searchMaterialVoList;
    }

    @Override
    public Result<List<Material>> getAllMaterial() {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        List<Material> materials = this.baseMapper.selectList(queryWrapper);
        return Result.ok(materials);
    }
}
